<template>
  <div class="pageDetail">
    <el-form
      ref="formRef"
      class="form_item_w2"
      :model="pageForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="课程名称" prop="courseName">
        <el-input v-model="pageForm.courseName" placeholder="请输入课程名称" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="课程类型" prop="courseTypeId">
        <el-select v-model="pageForm.courseTypeId" placeholder="请输入课程类型" :disabled="disabled">
          <el-option 
            v-for="(dictItem,index) in courseTypeDicts" 
            :label="dictItem.dictLabel" 
            :value="dictItem.dictCode"
            :key="index" 
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属专业群" prop="majorGroup">
        <el-input v-model="pageForm.majorGroup" placeholder="请输入所属专业群" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="所属学院">
        <TheDeptSelect v-model="pageForm.collageId" :modelValue="pageForm.collageId" :deptType="39" placeholder="请选择所属学院"  :disabled="disabled" @update:modelValue="pageForm.collageId = $event"/>
      </el-form-item>
      <el-form-item label="所属专业">
        <TheDeptSelect v-model="pageForm.majorId" :modelValue="pageForm.majorId" :deptType="40" placeholder="请选择所属专业"  :disabled="disabled" @update:modelValue="pageForm.majorId = $event"/>
      </el-form-item>
      <el-form-item label="所属班级">
        <TheDeptSelect v-model="pageForm.clazzId" :modelValue="pageForm.clazzId" :deptType="41" placeholder="请选择所属班级"  :disabled="disabled" @update:modelValue="pageForm.clazzId = $event"/>
      </el-form-item>
      <el-form-item label="职业岗位">
        <el-input v-model="pageForm.jobName" placeholder="请输入职业岗位" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="职业能力">
        <el-input type="textarea" v-model="pageForm.professionAbility" placeholder="请输入职业能力" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="课时" prop="courseHour">
        <el-input-number v-model="pageForm.courseHour" :min="0" label="请输入课时" :disabled="disabled"></el-input-number>
      </el-form-item>
      <el-form-item label="学分分布" prop="creditHourDistribute">
        <el-input v-model="pageForm.creditHourDistribute" placeholder="请输入学分分布" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="重点内容" prop="emphasesContent"  class="item_w1">
        <el-input type="textarea" v-model="pageForm.emphasesContent" placeholder="请输入重点内容" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="教学周次" prop="teachWeek">
        <el-input v-model="pageForm.teachWeek" placeholder="请输入教学周次" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="课程状态" prop="courseStatus">
        <el-select v-model="pageForm.courseStatus" placeholder="请输入课程类型" :disabled="disabled">
          <el-option 
            v-for="(dictItem,index) in courseStatusDicts" 
            :label="dictItem.dictLabel" 
            :value="dictItem.dictCode"
            :key="index" 
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学年学期" prop="academicYear">
        <el-input v-model="pageForm.academicYear" placeholder="请输入学年学期" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="考核方式" prop="checkType">
        <el-input v-model="pageForm.checkType" placeholder="请输入考核方式" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="开课时间" prop="startDate">
        <el-date-picker
          v-model="pageForm.startDate"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          :disabled="disabled">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结课时间" prop="endDate">
        <el-date-picker
          v-model="pageForm.endDate"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          :disabled="disabled">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="教案上传">
        <FileUpload v-model="pageForm.fileId" :disabled="disabled" :fileInfo="pageForm.fileInfo"/>
      </el-form-item>
    </el-form>
    <div class="pageFooterBtn">
      <el-button @click="$router.back()">返回</el-button>
      <el-button type="primary" @click="handleSubmit()" v-if="['add','edit'].includes(action)" :loading="loading">提交</el-button>
    </div>
  </div>
</template>
<script>
import { rules } from './data'
import { useDict } from '@/utils/dict/useDict'
import { coursePlanAddApi, coursePlanEditApi, coursePlanByIdApi } from '@/api/teachingIdea/teachingPlan'
import FileUpload from "@/components/FileUpload";
import TheDeptSelect from '@/componentsView/TheDeptSelect.vue';

export default {
  name: 'TeachingPlanDetail',
  components: { FileUpload, TheDeptSelect },
  data() {
    return {
      rules,
      pageForm: {
        collageId: null,
      },
      loading:false,
      disabled:false,
      courseTypeDicts: useDict('course_type'),
      courseStatusDicts: useDict('course_status'),
      planId: this.$route.query.planId,
      action: this.$route.query.action
    }
  },

  created() {
    if (this.planId) this.getCoursePlanById()
  },
  methods: {
    async handleSubmit() {
      const pureNumberRegex = /^[+-]?(\d+(\.\d*)?|\.\d+)$/;
      if (pureNumberRegex.test(this.pageForm.courseName)) {
        this.$message.error('课程名称不能为纯数字')
        return
      }
      try {
        await this.$refs.formRef.validate()
        this.loading = true
        let res = {}
        if (this.planId) {
          delete this.pageForm.fileInfo
          
          res = await coursePlanEditApi(this.pageForm)
        }else{
          res = await coursePlanAddApi(this.pageForm)
        }
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$router.back()
        } else {
          this.$message.error(res.msg || '操作失败')
        }
      } catch (error) {
        if (error !== 'validate') {
          this.$message.error(msg)
        }
      } finally {
        this.loading = false
      }
    },

    getCoursePlanById(){
      if (this.action === 'look') this.disabled = true
      coursePlanByIdApi(this.planId).then(res => {
        if (res.code === 200) {
          this.pageForm = Object.assign({},this.pageForm, res.data)
          
        } else {
          this.$message.error(res.msg)
        }
      })
    }
    
  },
}
</script>
<style scoped lang="scss">
::v-deep .el-select {
	width: 100% !important;
}
::v-deep .el-date-editor{
  width: 100% !important;
}
::v-deep .el-input-number{
  width: 100% !important;
}
</style>